package com.haierp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.mapper.AutomaticOrderDetailMapper;
import com.haierp.mapper.AutomaticOrderMapper;
import com.haierp.model.automatic.AutomaticOrder;
import com.haierp.model.automatic.AutomaticOrderDetail;
import com.haierp.service.IAutomaticOrderService;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.AutomaticQueryVO;

/**
 *
 * AutomaticOrder 表数据服务层接口实现类
 *
 */
@Service
public class AutomaticOrderServiceImpl extends SuperServiceImpl<AutomaticOrderMapper, AutomaticOrder> implements IAutomaticOrderService {

	@Autowired
	private AutomaticOrderDetailMapper automaticOrderDetailMapper;


	@Override
	public JsonPageResult<List<AutomaticOrder>> queryAutomatic(AutomaticQueryVO automaticQueryVO) {
		JsonPageResult<List<AutomaticOrder>> automaticResult = new JsonPageResult<>();
		Integer totalCount = this.baseMapper.queryAutomaticOrderCount(automaticQueryVO);
		if(totalCount!=null && totalCount!=0) {
			automaticResult.buildPage(totalCount, automaticQueryVO);
			List<AutomaticOrder> automaticOrderList = this.baseMapper.queryAutomaticOrder(automaticQueryVO);
			automaticResult.buildData(automaticOrderList);
		} else {
			List<AutomaticOrder> automaticOrderList = new ArrayList<>();
			automaticResult.buildData(automaticOrderList);
		}
		return automaticResult;
	}

	@Override
	public void addAutomaticOrder(AutomaticOrder automaticOrder) {
		this.baseMapper.insertSelective(automaticOrder);
		if(automaticOrder.getAutomaticOrderDetail() != null && !automaticOrder.getAutomaticOrderDetail().isEmpty()) {
			List<AutomaticOrderDetail> automaticOrderDetails = automaticOrder.getAutomaticOrderDetail();
			int index = 0;
			for (AutomaticOrderDetail automaticOrderDetail : automaticOrderDetails) {
				automaticOrderDetail.setAutoDetialNo("ZO"+automaticOrder.getAutoNo().substring(2)+String.format("%0"+4+"d", index));
				automaticOrderDetail.setAutomaticOrderId(automaticOrder.getId());
				automaticOrderDetail.setGmtCreate(new Date());
				automaticOrderDetail.setGmtModify(new Date());
				++index;
			}
			automaticOrderDetailMapper.insertBatch(automaticOrderDetails);
		}
	}

	@Override
	public AutomaticOrder queryUpdateAutomaticOrder(Long automaticId) {
		if(automaticId == null) {
			throw new RuntimeException("the automatic id is null");
		}
		AutomaticOrder automaticOrder = this.baseMapper.selectById(automaticId);
		if(automaticOrder != null) {
			EntityWrapper<AutomaticOrderDetail> entityWrapper = new EntityWrapper<>();
			entityWrapper.where("automatic_order_id = {0}", automaticId);
			List<AutomaticOrderDetail> automaticOrderDetailList = automaticOrderDetailMapper.selectList(entityWrapper);
			automaticOrder.setAutomaticOrderDetail(automaticOrderDetailList);
		}
		return automaticOrder;
	}

	@Override
	public void updateAutomaticOrder(AutomaticOrder automaticOrder) {
		this.baseMapper.updateSelectiveById(automaticOrder);
		List<AutomaticOrderDetail> automaticOrderDetailQT = automaticOrder.getAutomaticOrderDetail();
		List<AutomaticOrderDetail> automaticOrderDetailInsert = new ArrayList<>();
		List<AutomaticOrderDetail> automaticOrderDetailUpdate = new ArrayList<>();
		Set<Long> allDetails = new HashSet<Long>();
		EntityWrapper<AutomaticOrderDetail> entityWrapper = new EntityWrapper<>();
		entityWrapper.where("automatic_order_id = {0}", automaticOrder.getId());
		List<AutomaticOrderDetail> automaticOrderDetailHT = automaticOrderDetailMapper.selectList(entityWrapper);
		Integer index = automaticOrderDetailMapper.queryMaxautoDetialNoIndex(automaticOrder.getId());
		if(index == null) {
			index = 0;
		}
		for (AutomaticOrderDetail automaticDetailHT : automaticOrderDetailHT) {
			allDetails.add(automaticDetailHT.getId());
		}
		for (AutomaticOrderDetail automaticOrderDetail : automaticOrderDetailQT) {
			if(automaticOrderDetail.getId() == null) {
				automaticOrderDetail.setAutomaticOrderId(automaticOrder.getId());
				automaticOrderDetail.setAutoDetialNo("ZO"+automaticOrder.getAutoNo().substring(2)+String.format("%0"+4+"d", ++index));
				automaticOrderDetail.setGmtCreate(new Date());
				automaticOrderDetail.setGmtModify(new Date());
				automaticOrderDetailInsert.add(automaticOrderDetail);
			} else {
				if(!allDetails.isEmpty()) {
					allDetails.remove(automaticOrderDetail.getId());					
				}
				automaticOrderDetail.setGmtModify(new Date());
				automaticOrderDetailUpdate.add(automaticOrderDetail);
			}
		}
		if(!automaticOrderDetailInsert.isEmpty()) {
			automaticOrderDetailMapper.insertBatch(automaticOrderDetailInsert);
		}
		if(!automaticOrderDetailUpdate.isEmpty()) {
			automaticOrderDetailMapper.updateBatchById(automaticOrderDetailUpdate);
		}
		if(!allDetails.isEmpty()) {
			automaticOrderDetailMapper.deleteBatchIds(new ArrayList<Long>(allDetails));
		}
	}
}